﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Vibz.SharePoint.Query
{
    public enum ExpressionType
    {
        Value,
        Expression
    }
    public class Expression
    {
        public object ColumnName
        {
            get;
            set;
        }
        public object ColumnValue
        {
            get;
            set;
        }
        public object ColumnDataType
        {
            get;
            set;
        }

        internal Expression Expression1
        {
            get;
            set;
        }
        internal Expression Expression2
        {
            get;
            set;
        }
        internal Operator Operator
        {
            get;
            set;
        }
        internal Operand Operand
        {
            get;
            set;
        }

        internal ExpressionType ExpressionType = ExpressionType.Value;
        Expression() // Overriding default empty constructor to private
        {
        }
        Expression(Expression expression1, Expression expression2, Operand opr)
        {
            Expression1 = expression1;
            Expression2 = expression2;
            Operand = opr;

            ExpressionType = ExpressionType.Expression;
        }
        Expression(string colName, string colValue, string colType, Operator opr)
        {
            ColumnName = colName;
            ColumnValue = colValue;
            ColumnDataType = colType;
            Operator = opr;

            ExpressionType = ExpressionType.Value;
        }
        public static Expression Create(string colName, string colValue, Operator opr)
        {
            return new Expression(colName, colValue, "Text", opr);
        }
        public static Expression Create(Expression expression1, Expression expression2, Operand opr)
        {
            return new Expression(expression1, expression2, opr);
        }
        public static Expression Create(string colName, string colValue, string colType, Operator opr)
        {
            return new Expression(colName, colValue, colType, opr);
        }
        public static Expression Create<T>(string colName, T[] listVal)
        {
            if(listVal.Length == 0)
                return null;
            Expression exp2 = (listVal.Length == 1)
                ? Expression.Create(colName, listVal[0].ToString(), Operator.Eq)
                : Expression.Create(colName, listVal.Except(new T[] { listVal[0] }).ToArray());
            return Expression.Create(
                Expression.Create(colName, listVal[0].ToString(), Operator.Eq),
                exp2, 
                Operand.Or
                );
        }

        public static Expression Create(KeyValuePair<string, object>[] columnValueList)
        {
            if(columnValueList.Length == 0)
                return null;
            KeyValuePair<string, object> item = columnValueList.ElementAt(0);
            Expression exp2 = (columnValueList.Length == 1)
                ? Expression.Create(item.Key, item.Value.ToString(), Operator.Eq)
                : Expression.Create(columnValueList.Where(cList=>cList.Key!=item.Key).ToArray());
            return Expression.Create(
                Expression.Create(item.Key, item.Value.ToString(), Operator.Eq),
                exp2,
                Operand.And
                );
        }
    }
}
